import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPool{
    //阻塞队列
    private BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(10);
    public void submit(Runnable runnable) throws InterruptedException {
        blockingQueue.put(runnable);
    }
    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(() -> {
                try {
                    Runnable tmp = blockingQueue.take();
                    tmp.run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
        }
    }
}
public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(4);
        for (int i = 0; i < 100; i++) {
            int id = i;
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(id);
                }
            });
        }
    }
}
